<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>async 与 AJAX 封装 AJAX请求</title>
</head>
<body>
    <script>
      // "GET", "https://api.apiopen.top/api/sentences"
      function sendAJAX(){
         return new Promise((resolve,reject)=>{
            //1、创建对象
            const x = new XMLHttpRequest();
            //2、初始化
            x.open("GET", "https://api.apiopen.top/api/sentences");
            //3、发送
            x.send();
            //4、事件绑定
            x.onreadystatechange = function(){
                    if(x.readyState === 4){
                        if(x.status>=200 && x.status<300){
                            //成功啦
                            resolve(x.response);
                        }else{
                            //如果失败
                            reject(x.status);
                        }
                    }
                 }
          })
        
      }

    // promise then 方法测试
    //   sendAJAX().then(value=>{
    //     console.log(value);
    //   },reason=>{
    //     console.log(reason);
    //   });

    // async 与 await 测试
    async function  main() {
        //发送 Ajax
        let r = await sendAJAX();
        console.log(r);
    }
      
    main();

    </script>
</body>
</html>